package com.pwx.student.utils;

import com.pwx.student.common.Const;
import com.pwx.student.vo.output.LoginOutputVo;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;

/**
 * Copyright (C),2021-2022,亚瑟王
 * FileName: TokenUtils
 * Author: saber
 * Date:2022/10/26 17:49
 */
public class TokenUtils {

    public static String getToken(LoginOutputVo outputVo,Long time){
        long currTime = System.currentTimeMillis();

        return Jwts.builder()
                .claim("type",outputVo.getType().toString())
                .setId(outputVo.getUserId().toString())
                .setSubject(outputVo.getUsername())
                .setIssuedAt(new Date(currTime))
                .setExpiration(new Date(currTime+time))
                .signWith(SignatureAlgorithm.HS256, Const.TOKEN_SING)
                .compact();
    }

    public static Claims parseToken(String token){
        return (Claims) Jwts.parser().setSigningKey(Const.TOKEN_SING)
                .parse(token).getBody();
    }

} 
